package com.relax.t1;

public class TheadDemo implements Runnable {
    public synchronized void run() {
        while (true) {
            System.out.println("子线程执行");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        TheadDemo demo = new TheadDemo();
        Thread t = new Thread(demo);
        t.start();

        while (true) {
            synchronized (demo){
                System.out.println("main程执行");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                demo.notifyAll();
            }

        }


    }
}
